//
// Created by tonystark on 16/3/22.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "BaseController.h"
#import "Define.h"

@interface BaseController () {

}
@property(nonatomic, strong) UIButton *backBtn;
@end

@implementation BaseController {

}
- (void)dealloc {
    DEBUG_LOG(@"%@ dealloc", NSStringFromClass([self class]));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self topView];
}

#pragma mark - Status Bar

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

#pragma mark - nav bar

- (UIView *)topView {
    if (!_topView) {
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, GAP20, deviceWidth, topBarHeight)];
        [self.view addSubview:topView];

        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -GAP20, deviceWidth, topBarHeight + GAP20)];
        bgView.backgroundColor = topBarColor;
        bgView.tag = kTopBgViewTag;
        [topView addSubview:bgView];

        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.frame = CGRectMake(44, (topView.frameHeight - topBarHeight) / 2, deviceWidth - 88, topBarHeight);
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = [self topTitle];
        [topView addSubview:titleLabel];

        _titleLabel = titleLabel;
        _topView = topView;
    }
    return _topView;
}

- (NSString *)topTitle {
    return @"";
}

- (void)pushController:(UIViewController *)controller {
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark Create Base Button

- (void)addBackBtn {
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"but_back_nor"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"but_back_sel"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.topView);
        make.leftMargin.mas_equalTo(4);
    }];
    _backBtn = backBtn;
}

- (void)backBtnPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIButton *)customRightTextBtn:(NSString *)text action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.topView addSubview:btn];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    CGFloat textWidth = ceilf([btn.titleLabel sizeThatFits:CGSizeZero].width) + 10;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(textWidth);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.topView);
        make.rightMargin.mas_equalTo(-4);
    }];
    return btn;
}

- (UIButton *)customRightImgBtn:(UIImage *)img1 img2:(UIImage *)img2 action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.topView addSubview:btn];
    [btn setImage:img1 forState:UIControlStateNormal];
    [btn setImage:img2 forState:UIControlStateHighlighted];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.topView);
        make.rightMargin.mas_equalTo(-4);
    }];
    return btn;
}

@end